#include "network.h"
#include "config.h"

int init_sock( int *fd ) {
	*fd = socket( AF_INET, SOCK_STREAM, 0 );
	if( *fd < 0 )
		panic( "create listenfd" );
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons( LISTEN_PORT );
	addr.sin_addr.s_addr = INADDR_ANY;
	int opt=1;
	setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR, ( char * )&opt, sizeof( opt ) );
	if( ( bind( *fd, (struct sockaddr*)&addr, sizeof( addr ) ) < 0 ) )
		panic( "bind" );
	listen_sock( *fd );
	return *fd;
}

void listen_sock( int fd ) {
	if( ( listen( fd, 16 ) ) < 0 )
		panic( "listen" );
}

